home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / commio0b.zip / CMDLINE.PAS < prev    next >
Pascal/Delphi Source File  |  1996-01-04  |  5KB  |  146 lines

  1. {
  2. From: dissel@nunic.nu.edu (David S. Issel)
  3. Modified by: Lief O'Pardy
  4.  
  5. To use it, simply put the USES CMDLINE; in your program.
  6.  
  7. Example1:  If you entered:  MYPROG /x/y/z="this is a test"
  8. TurboPascal would respond:  ParamStr(x) Contents
  9.                             =========== =================
  10.                             1           /x/y/z="this
  11.                             2           is
  12.                             3           a
  13.                             4           test"
  14.  
  15. My unit would respond:      1           /X
  16.                             2           /Y
  17.                             3           /Z=this is a test
  18.  
  19.  
  20. Example2:  If you entered:  MYPROG file1,file2,file3
  21. TurboPascal would respond:  ParamStr(x) Contents
  22.                             =========== =================
  23.                             1           file1,file2,file3
  24.  
  25. My unit would respond:      1           FILE1
  26.                             2           FILE2
  27.                             3           FILE3
  28.  
  29. My unit replaces the ParamCount variable and ParamStr() function.
  30. The original TurboPascal routines are retained as System.ParamCount and
  31. System.ParamStr()}
  32.  
  33. Unit CMDLINE;  { Written by David S. Issel, 1989 }
  34.  
  35. Interface  { public }
  36.  
  37. type
  38.   string127 = string[127];
  39.  
  40. Var
  41.   ParamCount:integer;
  42.  
  43. Function ParamStr(Param:word):string127;
  44. {^ Just like TP's ParamStr.  But better. }
  45. Function ScanParam(st:string127; casesen:boolean):boolean;
  46. {^ Scan the command line for a certain "switch."
  47.     st      = switch to search for.
  48.     casesen = if TRUE then the string being searched for will have to match
  49.               "st" exactly (any upper and lower case chars), otherwise the
  50.               case will not matter. }
  51.  
  52.  
  53. Implementation  { private }
  54.  
  55. uses doorio; {for upcasestr}
  56.  
  57. Var
  58.   ParamArray:array[1..62] of string127;
  59.  
  60. {───────────────────────────────────────────────────────────────────────────}
  61. Function ScanParam; {added by lief o'pardy}
  62. var i:byte; b:boolean;
  63. begin
  64.   b:=false;
  65.   for i := 1 to ParamCount do begin
  66.     if casesen then begin
  67.       b:=(st=ParamStr(i));
  68.     end else begin
  69.       b:=(upcasestr(st)=upcasestr(ParamStr(i)));
  70.     end;
  71.     if b then break;
  72.   end;
  73.   ScanParam:=b;
  74. end;
  75. {───────────────────────────────────────────────────────────────────────────}
  76. Function ParamStr;
  77.   begin
  78.     if Param<=ParamCount
  79.       then ParamStr:=ParamArray[Param]
  80.       else ParamStr:='';
  81.   end;
  82. {───────────────────────────────────────────────────────────────────────────}
  83. Procedure SetupParamArray;
  84.   var
  85.     Index:word;
  86.     WorkStr:string;
  87.   procedure TxfrString;
  88.     var
  89.       SrchChar:string;
  90.     begin
  91.       SrchChar:=WorkStr[Index];
  92.       Inc(Index);
  93.       while (Index<=Length(WorkStr)) and (WorkStr[Index]<>SrchChar) do
  94.         begin
  95.           ParamArray[ParamCount]:=ParamArray[ParamCount]+WorkStr[Index];
  96.           Inc(Index);
  97.         end;
  98.       if Index<=Length(WorkStr)
  99.         then Inc(Index);
  100.     end;
  101.   begin
  102.     ParamCount:=0;
  103.     if System.ParamCount<1 then Exit;
  104.     WorkStr:=System.ParamStr(1);
  105.     if System.ParamCount>1
  106.       then for Index:=2 to System.ParamCount do
  107.               WorkStr:=WorkStr+' '+System.ParamStr(Index);
  108.     Index:=1;
  109.     repeat
  110.       Inc(ParamCount);
  111.       ParamArray[ParamCount]:='';
  112.       if (WorkStr[Index]=#34) or (WorkStr[Index]=#39)
  113.         then TxfrString
  114.         else
  115.           begin
  116.             if WorkStr[Index]<>','
  117.               then ParamArray[ParamCount]:=ParamArray[ParamCount]+
  118.                                            Upcase(WorkStr[Index]);
  119.             Inc(Index);
  120.             if Index<=Length(WorkStr)
  121.               then
  122.                 begin
  123.                   while (Index<=Length(WorkStr)) and (WorkStr[Index]<>#47)
  124.                       and (WorkStr[Index]<>#32) and (WorkStr[Index]<>#34)
  125.                       and (WorkStr[Index]<>#39) and (WorkStr[Index]<>#44)
  126.                     do
  127.                       begin
  128.                         ParamArray[ParamCount]:=ParamArray[ParamCount]+
  129.                                                 Upcase(WorkStr[Index]);
  130.                         Inc(Index);
  131.                       end;
  132.                   if (Index<=Length(WorkStr)) and ((WorkStr[Index]=#34)
  133.                       or (WorkStr[Index]=#39))
  134.                     then TxfrString;
  135.                 end;
  136.           end;
  137.       while (Index<=Length(WorkStr)) and (WorkStr[Index]=#32) do
  138.         Inc(Index);
  139.     until Index>Length(WorkStr);
  140.   end;
  141.  
  142. begin  { Initialization Code }
  143.   SetupParamArray;
  144. end.
  145.  
  146.